<template>
  <div class="container">
    <el-row type="flex" class="row-bg" justify="space-between">
      <el-col>
        <el-form :inline="true" :model="page">
          <el-form-item label="目录名称">
            <el-input
              v-model="page.directoryName"
              placeholder="审批人"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="page.state">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="" @click="cls">清除</el-button>
            <el-button type="primary" @click="getList">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col>
        <el-row type="flex" justify="end">
          <el-button v-show="routerVla.id" @click="$router.back()"
            >返回学科</el-button
          >
          <el-button type="success" @click="isDiaLog"> 新增目录</el-button>
        </el-row>
      </el-col>
    </el-row>
    <div class="warning">
      <i class="el-icon-info"></i>
      共{{ counts }}条数据
    </div>
    <el-table :data="list" style="width: 100%" class="table">
      <el-table-column label="序号" width="50" type="index"> </el-table-column>
      <el-table-column prop="subjectName" label="所属学科" width="180">
      </el-table-column>
      <el-table-column prop="directoryName" label="目录名称"> </el-table-column>
      <el-table-column prop="username" label="创建者" width="180">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="180">
      </el-table-column>
      <el-table-column prop="totals" label="面试题数量"> </el-table-column>
      <el-table-column prop="state" label="状态" width="180">
        <template slot-scope="{ row }">
          <span v-if="row.state === 1">已启用</span>
          <span v-else> 已禁用 </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="text" size="small" @click="stop(row)">
            {{ row.state === 1 ? "启用" : "禁用" }}
          </el-button>
          <el-button type="text" size="small" v-if="row.state === 1"
            >修改</el-button
          >
          <el-button type="text" size="small" v-else disabled>修改</el-button>
          <el-button
            type="text"
            size="small"
            v-if="row.state === 1"
            @click="dle(row.id)"
            >移除</el-button
          >
          <el-button type="text" size="small" v-else disabled>移除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
      <el-col :span="10">
        <el-pagination
          @current-change="changePage"
          background
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="prev, pager, next, sizes , jumper"
          :total="counts"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <directorys-add :isShow.sync="showDialog" @list="getList" />
  </div>
</template>

<script>
import { list, changeState, remove } from "../../api/hmmm/directorys";
import DirectorysAdd from "../components/directorys-add";
export default {
  data() {
    return {
      routerVla: this.$route.query,
      counts: 0,
      list: [],
      page: {
        directoryName: "",
        state: null,
        page: 1,
        size: 10
      },
      showDialog: false,
      disabledShow: true
    };
  },
  components: {
    DirectorysAdd
  },
  created() {
    console.log(this.routerVla);
    if (this.routerVla) {
      this.routrLis();
    } else {
      this.getList();
    }
  },
  methods: {
    async routrLis() {
      const { data } = await list(this.routerVla);
      (this.counts = data.counts), (this.list = data.items);
    },
    async getList() {
      const { data } = await list(this.page);
      this.counts = data.counts;
      this.list = data.items;
    },
    changePage(newPage) {
      this.page.page = newPage;
      this.getList();
    },
    isDiaLog() {
      this.showDialog = true;
    },
    cls() {
      this.page.directoryName = "";
      this.page.state = null;
    },
    async stop(row) {
      this.disabledShow = !this.disabledShow;
      const data = await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      });
      row.state = row.state === 1 ? 0 : 1;
    },
    async dle(id) {
      try {
        await this.$confirm("确认删除吗？");
        await remove({ id });
        this.$message.success("删除成功");
        this.getList();
      } catch (error) {
        console.log(error);
      }
    }
  }
};
</script>

<style scoped lang="css">
.container {
  padding: 20px;
  background-color: #fff;
}
.warning {
  margin-bottom: 15px;
  width: 100%;
  height: 35px;
  background-color: rgb(247, 245, 246);
  border-radius: 3px;
  line-height: 35px;
  font-size: 9px;
  color: rgb(194, 193, 193);
}
.table {
  margin: 20px 0;
}
</style>
